package com.xiashitech.apiservice.service.impl;

import com.google.common.collect.Maps;
import com.xiashitech.apiservice.config.AuthConfig;
import com.xiashitech.apiservice.dao.DataWaterMarkConfigMapper;
import com.xiashitech.apiservice.dao.model.DataWaterMarkConfig;
import com.xiashitech.apiservice.dto.Pager;
import com.xiashitech.apiservice.dto.Result;
import com.xiashitech.apiservice.service.IDataWaterMarkConfigService;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.var;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class DataWaterMarkServiceConfigImpl implements IDataWaterMarkConfigService {
    private final AuthConfig authConfig;
    private final DataWaterMarkConfigMapper dataWaterMarkConfigMapper;

    @Override
    public int insertDataWaterMarkConfig(DataWaterMarkConfig dataWaterMarkConfig) {
        return dataWaterMarkConfigMapper.insertDataWaterMarkConfig(dataWaterMarkConfig);
    }

    @Override
    public DataWaterMarkConfig getDataWaterMarkConfigByPath(String apiPath) {
        return dataWaterMarkConfigMapper.getDataWaterMarkConfigByPath(apiPath);
    }

    @Override
    public DataWaterMarkConfig getDataWaterMarkConfigById(Long id) {
        return dataWaterMarkConfigMapper.getDataWaterMarkConfigById(id);
    }

    @Override
    public List<DataWaterMarkConfig> getDataWaterMarkConfigListPage(Pager<DataWaterMarkConfig> pager) {
        return dataWaterMarkConfigMapper.getDataWaterMarkConfigListPage(pager);
    }

    public List<DataWaterMarkConfig> getDataWaterMarkConfigList(DataWaterMarkConfig dataWaterMarkConfig) {
        return dataWaterMarkConfigMapper.getDataWaterMarkConfigList(dataWaterMarkConfig);
    }

    @Override
    public int updateDataWaterMarkConfig(DataWaterMarkConfig dataWaterMarkConfig) {
        return dataWaterMarkConfigMapper.updateDataWaterMarkConfig(dataWaterMarkConfig);
    }

    @Override
    public int deleteDataWaterMarkConfig(List<Long> ids) {
        return dataWaterMarkConfigMapper.deleteDataWaterMarkConfig(ids);
    }

    @Override
    public long getDataWaterMarkConfigCount(Map<String, Object> conditions) {
        return dataWaterMarkConfigMapper.getDataWaterMarkConfigCount(conditions);
    }

    @Override
    public Result<JSONObject> getWaterMarkMode(String systemId) {
        Result<JSONObject> result = Result.failure();
        Map<String, Object> params = Maps.newHashMap();
        params.put("system", systemId);
        params.put("type", "data");
        HttpResponse httpResponse = HttpRequest.get(authConfig.getApiUrlForGetWaterMarkStatus())
                .form(params).timeout(30 * 1000).execute();
        if (httpResponse.isOk() && JSONUtil.isTypeJSON(httpResponse.body())) {
            var r = JSONUtil.parseObj(httpResponse.body());
            if (r.isEmpty())
                return result;
            String[] keysToCheck = { "code", "message", "data", "result" };
            boolean notNull = Arrays.stream(keysToCheck).map(key -> r.get(key)).allMatch(value -> value != null);
            if (notNull)
                result.setStatus(r.getInt("code")).setMessage(r.getStr("message"))
                        .setData(JSONUtil.parseObj(r.get("data")));
        }
        return result;
    }
}
